package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.SalesStatistics;
import com.example.mall.dto.SalesStatisticsDto;
import com.example.mall.mapper.SalesStatisticsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SalesStatisticsQuery;
import com.example.mall.service.ISalesStatisticsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 销售数据统计表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class SalesStatisticsServiceImpl extends ServiceImpl<SalesStatisticsMapper, SalesStatistics> implements ISalesStatisticsService {

    @Autowired
    private SalesStatisticsMapper salesStatisticsMapper;

    @Override
    public PageDTO<SalesStatisticsDto> queryPage(SalesStatisticsQuery salesStatisticsQuery) {
        if (salesStatisticsQuery == null) {
            salesStatisticsQuery = new SalesStatisticsQuery();
        }
        Page<SalesStatistics> page = salesStatisticsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SalesStatistics> cartPage = lambdaQuery()
                .like(salesStatisticsQuery.getTotalSales()!= null, SalesStatistics::getTotalSales, salesStatisticsQuery.getTotalSales())
                .like(salesStatisticsQuery.getOrderCount()!= null, SalesStatistics::getOrderCount, salesStatisticsQuery.getOrderCount())
                .like(salesStatisticsQuery.getStatId()!= null, SalesStatistics::getStatId, salesStatisticsQuery.getStatId())
                .like(salesStatisticsQuery.getStatPeriod()!= null, SalesStatistics::getStatPeriod, salesStatisticsQuery.getStatPeriod())
                .apply(salesStatisticsQuery.getStatDate() != null,
                        "DATE_FORMAT(stat_date, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        salesStatisticsQuery.getStatDate())
                .orderByDesc(SalesStatistics::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, SalesStatisticsDto.class);
    }

    @Override
    public SalesStatistics querySalesStatisticsById(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        return salesStatisticsMapper.selectById(id);
    }

    @Override
    public List<SalesStatistics> querySalesStatisticsList() {
        return this.list();
    }

    @Override
    public void insert(SalesStatisticsDto salesStatisticsDto) {
        SalesStatistics salesStatistics = new SalesStatistics();
        CopyUtils.copyProperties(salesStatisticsDto,salesStatistics);
        salesStatistics.setStatId(CodeGenerateUtils.generateUniqueId());
        salesStatistics.setCreateDate(new Date());
        salesStatistics.setUpdateDate(new Date());
        salesStatisticsMapper.insert(salesStatistics);
    }

    @Override
    public void update(SalesStatisticsDto salesStatisticsDto) {
        AssertUtil.isNotNull(salesStatisticsDto.getStatId(),"唯一标识不能为空");
        SalesStatistics salesStatistics = salesStatisticsMapper.selectById(salesStatisticsDto.getStatId());
        AssertUtil.isNotNull(salesStatistics,"销售数据统计数据不存在");
        CopyUtils.copyProperties(salesStatisticsDto,salesStatistics);
        salesStatistics.setUpdateDate(new Date());
        salesStatisticsMapper.updateById(salesStatistics);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        SalesStatistics salesStatistics = salesStatisticsMapper.selectById(id);
        AssertUtil.isNotNull(salesStatistics,"销售数据统计数据不存在");
        salesStatisticsMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids,"唯一标识不能为空");
        List<SalesStatistics> salesStatistics = salesStatisticsMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(salesStatistics,"销售数据统计数据不存在");
        salesStatisticsMapper.deleteBatchIds(ids);
    }
}
